package com.heima.article.listener;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.WmNewsUpDownConstants;
import com.heima.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

import static com.heima.common.constants.message.WmNewsUpDownConstants.*;

/**
 * @Author RMY
 * @Date 2021/11/30 22:05
 * @Version 1.0
 */
@Slf4j
@Component
public class WmNewsUpDownListener {
    @Autowired
    private ApArticleService apArticleService;
    //打上注解，定义方法
    @KafkaListener(topics = WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMsg(ConsumerRecord<String,String> record){
        Optional<ConsumerRecord<String,String>> optional = Optional.ofNullable(record);

        if (optional.isPresent()){
            //取出值
            String value = record.value();
            log.info("article接收到信息"+value);
            Map<String, Long> map = JsonUtils.toMap(value, String.class, Long.class);
            //调用service
            apArticleService.upOrDown(map);
        }
    }
}
